package com.huajin.codetest.jvm;

/** 
 * Escape Analysis
 * 逃逸分析
 */
public class EscapeAnalysisTest {
	
	public static Object object;
	
	/**
	 * StringBuilder可能被其他方法改变，逃逸到了方法外部
	 */
	public StringBuilder escape(String a, String b) {
		StringBuilder sb = new StringBuilder();
		sb.append(a);
		sb.append(b);
		return sb;
	}
	
	/**
	 * 不直接返回StringBuilder，不发生逃逸
	 */
	public String notEscape(String a, String b) {
		StringBuilder sb = new StringBuilder();
		sb.append(a);
		sb.append(b);
		return sb.toString();
	}
	
	/**
	 * 外部线程可见object，发生逃逸
	 */
	public void objectEscape() {
		object = new Object();
	}
	
	/**
	 * 仅方法内部可见，不发生逃逸
	 */
	public void objectNotEscape() {
		Object object = new Object();
	}

}
